const mysql = require('mysql');
const db = mysql.createPool({
    host: "127.0.0.1",
    user: 'root',
    password: 'root',
    port: 3306,
    database: "yoho.net"
})


module.exports = {
    execute(sql, params, callback) {
        db.getConnection(function (err, conn) {
            if (err) console.log("链接数据库失败:" + err.message);
            conn.query(sql, params, function (err, result, fields) {
                if (err) {
                    if (err) console.log("执行失败:" + err.message);
                }
                callback(result, fields);
                conn.release();
            })
        })
    },

    executeAsync(sql, params) {
        return new Promise(function (resolve, reject) {
            db.getConnection(function (err, conn) {
                if (err) console.log("链接数据库失败:" + err.message);
                conn.query(sql, params, function (err, result, fields) {
                    if (err) {
                        if (err) console.log("执行失败:" + err.message);
                        reject(err.message);
                    }
                    resolve(result, fields);
                    conn.release();
                })
            })
        })
    }

}